{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:27:11.324787Z",
     "iopub.status.busy": "2023-11-13T13:27:11.324569Z",
     "iopub.status.idle": "2023-11-13T13:27:24.244203Z",
     "shell.execute_reply": "2023-11-13T13:27:24.243538Z",
     "shell.execute_reply.started": "2023-11-13T13:27:11.324763Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from datetime import datetime\n",
    "import os\n",
    "\n",
    "stage = 'B'\n",
    "root = '../../../contest'\n",
    "\n",
    "df_train = pd.read_csv(os.path.join(root, 'train/GSLD_MB_QRYTRNFLW.csv'))\n",
    "df_test = pd.read_csv(os.path.join(root, '{}/GSLD_MB_QRYTRNFLW_{}.csv'.format(stage, stage)))\n",
    "\n",
    "save_path = '../data'\n",
    "\n",
    "end_date_train = datetime(1996,7,5)\n",
    "end_date_test = datetime(1996,9,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:27:24.245590Z",
     "iopub.status.busy": "2023-11-13T13:27:24.245381Z",
     "iopub.status.idle": "2023-11-13T13:27:24.255377Z",
     "shell.execute_reply": "2023-11-13T13:27:24.254721Z",
     "shell.execute_reply.started": "2023-11-13T13:27:24.245564Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# trn_code_list = df_train.TRANSCODE.value_counts(ascending=False).index\n",
    "# trn_code_dic = {trn_code_list[i]:i for i,_ in enumerate(trn_code_list)}\n",
    "def prepro(df, end_date, offset):\n",
    "    \n",
    "#     df.TRANSCODE = df.TRANSCODE.map(trn_code_dic)\n",
    "#     cust_no_df = df[['CUST_NO']].drop_duplicates()\n",
    "#     for i in range(len(trn_code_dic)):\n",
    "#         tmp_df = df[df.TRANSCODE==i].groupby('CUST_NO').size().reset_index().rename(columns={0:'trn_'+str(i)+'_cnt'})\n",
    "#         cust_no_df = cust_no_df.merge(tmp_df, how='left', on='CUST_NO')\n",
    "#     cust_no_df = cust_no_df.fillna(0)\n",
    "\n",
    "    df['DATE'] = pd.to_datetime(df['DATE'], format='%Y%m%d')\n",
    "    df['weekday'] = (df['DATE'].dt.dayofweek+7+offset) % 7 # 当周星期几 脱敏数据存在偏移\n",
    "    tmp_df = df.groupby('CUST_NO').agg(\n",
    "        qrytrnflw_last_date = ('DATE', 'max'), # 活跃的最后一天\n",
    "        qry_trn_cd_nunique = ('TRANSCODE', 'nunique'),\n",
    "#         qrytrnflw_exist_weekend = ('weekday', 'max'), # 是否存在周末活跃记录\n",
    "        qrytrnflw_cust_cnt = ('weekday', 'count'), # 总条数\n",
    "    ).reset_index()\n",
    "    \n",
    "    tmp_df['qrytrnflw_last_date'] = pd.to_datetime(tmp_df['qrytrnflw_last_date'], format='%Y%m%d')\n",
    "#     tmp_df['qrytrnflw_last_date_weekday'] = (tmp_df['qrytrnflw_last_date'].dt.dayofweek+7+offset) % 7 # 最后一天星期几 \n",
    "    \n",
    "    tmp_df['qrytrnflw_last_date'] = (end_date - tmp_df['qrytrnflw_last_date']).dt.days\n",
    "    \n",
    "#     tmp_df['qrytrnflw_exist_weekend'] = tmp_df['qrytrnflw_exist_weekend'].apply(lambda x: 1 if x==6 or x==5 else 0)\n",
    "    \n",
    "    # 计算在周末活跃的次数\n",
    "    tmp_df2 = df[((df.weekday==5)|(df.weekday==6))].groupby('CUST_NO').agg(\n",
    "        qrytrnflw_weekend_cnt = ('weekday', 'count')\n",
    "    ).reset_index() \n",
    "    \n",
    "    tmp_df = tmp_df.merge(tmp_df2, how='left', on='CUST_NO')\n",
    "    tmp_df['qrytrnflw_weekend_cnt'] = tmp_df.qrytrnflw_weekend_cnt.fillna(0)\n",
    "    tmp_df['qrytrnflw_weekend_ratio'] = tmp_df.qrytrnflw_weekend_cnt / tmp_df.qrytrnflw_cust_cnt # 在周末活跃的比率\n",
    "    \n",
    "#     del tmp_df['qrytrnflw_cust_cnt'], tmp_df['qrytrnflw_weekend_cnt']\n",
    "    \n",
    "#     tmp_df = tmp_df.merge(cust_no_df, how='left', on='CUST_NO')\n",
    "\n",
    "    return tmp_df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:27:24.256611Z",
     "iopub.status.busy": "2023-11-13T13:27:24.256433Z",
     "iopub.status.idle": "2023-11-13T13:27:40.599691Z",
     "shell.execute_reply": "2023-11-13T13:27:40.599036Z",
     "shell.execute_reply.started": "2023-11-13T13:27:24.256589Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "df_train = prepro(df_train, end_date_train, 0)\n",
    "df_test = prepro(df_test, end_date_test, -3) # 测试集星期偏了三天"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:27:40.600914Z",
     "iopub.status.busy": "2023-11-13T13:27:40.600719Z",
     "iopub.status.idle": "2023-11-13T13:27:40.963039Z",
     "shell.execute_reply": "2023-11-13T13:27:40.962427Z",
     "shell.execute_reply.started": "2023-11-13T13:27:40.600890Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "df_train.to_csv(save_path+'/GSLD_MB_QRYTRNFLW.csv', index=False)\n",
    "df_test.to_csv(save_path+'/GSLD_MB_QRYTRNFLW_{}.csv'.format(stage), index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
